package com.junlong0716.enjoylearning.adapter

import android.annotation.SuppressLint
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.htxcsoft.corelibrary.glide.GlideUtils
import com.junlong0716.enjoylearning.CommentPresenter
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.constant.NetConstant.REQUEST_SOURCE
import com.junlong0716.enjoylearning.entity.*
import com.junlong0716.enjoylearning.entity.FindMultipleItemEntity.Companion.FIND_TYPE_COMMEND_AUDIO
import com.junlong0716.enjoylearning.entity.FindMultipleItemEntity.Companion.FIND_TYPE_COMMEND_DOCUMENT
import com.junlong0716.enjoylearning.entity.FindMultipleItemEntity.Companion.FIND_TYPE_COMMEND_TOPIC
import com.junlong0716.enjoylearning.entity.FindMultipleItemEntity.Companion.FIND_TYPE_COMMEND_VIDEO
import com.junlong0716.enjoylearning.entity.FindMultipleItemEntity.Companion.FIND_TYPE_HEADER
import com.junlong0716.enjoylearning.manager.GlideBannerImageLoader
import com.junlong0716.enjoylearning.ui.*
import com.junlong0716.enjoylearning.widget.RoundedBanner
import com.junlong0716.getknowledgedemo.decoration.DividerItemNoEndDecoration
import java.text.SimpleDateFormat


/**
 *@author: 巴黎没有摩天轮Li
 *@description: 首页发现界面列表 adapter
 *@date: Created in 下午12:59 2018/4/21
 *@modified by:
 */
class FindMultipleItemAdapter(data: ArrayList<FindMultipleItemEntity>, itemChildClickListener: ItemChildClickListener) : BaseMultiItemQuickAdapter<FindMultipleItemEntity, BaseViewHolder>(data) {
    private var mItemChildClickListener = itemChildClickListener

    init {
        addItemType(FindMultipleItemEntity.FIND_TYPE_HEADER, R.layout.item_find_header_layout)
        addItemType(FindMultipleItemEntity.FIND_TYPE_COMMEND_TOPIC, R.layout.item_find_commend_topic_layout)
        addItemType(FindMultipleItemEntity.FIND_TYPE_COMMEND_VIDEO, R.layout.item_find_commend_video_layout)
        addItemType(FindMultipleItemEntity.FIND_TYPE_COMMEND_AUDIO, R.layout.item_find_commend_audio_layout)
        addItemType(FindMultipleItemEntity.FIND_TYPE_COMMEND_DOCUMENT, R.layout.item_find_commend_document_layout)
    }

    override fun convert(helper: BaseViewHolder?, item: FindMultipleItemEntity?) {
        when (helper!!.itemViewType) {
            FIND_TYPE_HEADER -> {
                //配置banner
                val imageUrl: ArrayList<String> = ArrayList()
                val types = ArrayList<Int>()
                val knowledgeIds = ArrayList<Int>()
                val urlAddress = ArrayList<String>()
                for (i in 0 until item!!.data.size) {
                    for (j in 0 until item.data[i].bannerData.size) {

                        imageUrl.add(item.data[i].bannerData[j].imageUrl)
                        types.add(item.data[i].bannerData[j].type)
                        knowledgeIds.add(item.data[i].bannerData[j].specialId)
                        urlAddress.add(item.data[i].bannerData[j].urlAddress)
                        //imageUrl.add("http://123.127.109.109:8090/file/temp/banner/banner04.jpg")
                    }
                }
                val roundedBanner = helper.getView<RoundedBanner>(R.id.round_banner)
                roundedBanner.setImageLoader(GlideBannerImageLoader())
                roundedBanner.setImages(imageUrl)
                roundedBanner.setDelayTime(5000)
                roundedBanner.start()

                roundedBanner.setOnBannerListener { position ->

                    val commentPresenter = CommentPresenter()
                    when (types[position]) {
                        1 -> {
                            commentPresenter.requestKnowledgeDetail(knowledgeIds[position], object : CommentPresenter.RequestKnowledgeDetailCallback {
                                override fun requestKnowledgeDetailsSucceed(knowledgeTitle: String, knowledgeContent: String, createDate: Long, browseNum: Int, isCollect: Int, isPraise: Int, praiseNum: Int, coverImg: String, author: String, knowledgeDescribe: String, fileAddress: String) {
                                    val intent = Intent()
                                    intent.setClass(mContext, VideoActivity::class.java)
                                    intent.putExtra("video_url", REQUEST_SOURCE + fileAddress)
                                    intent.putExtra("video_id", knowledgeIds[position])
                                    intent.putExtra("video_author", author)
                                    mContext.startActivity(intent)
                                }
                            })
                        }
                        2 -> {
                            commentPresenter.requestKnowledgeDetail(knowledgeIds[position], object : CommentPresenter.RequestKnowledgeDetailCallback {
                                override fun requestKnowledgeDetailsSucceed(knowledgeTitle: String, knowledgeContent: String, createDate: Long, browseNum: Int, isCollect: Int, isPraise: Int, praiseNum: Int, coverImg: String, author: String, knowledgeDescribe: String, fileAddress: String) {
                                    val audioEntity = AudioEntity(knowledgeIds[position],REQUEST_SOURCE+fileAddress,knowledgeTitle,NetConstant.REQUEST_SOURCE + coverImg)
                                    mItemChildClickListener.setOnAudioItemChildClickListener(audioEntity)
                                }
                            })
                        }
                        3 -> {
                            commentPresenter.requestKnowledgeDetail(knowledgeIds[position], object : CommentPresenter.RequestKnowledgeDetailCallback {
                                override fun requestKnowledgeDetailsSucceed(knowledgeTitle: String, knowledgeContent: String, createDate: Long, browseNum: Int, isCollect: Int, isPraise: Int, praiseNum: Int, coverImg: String, author: String, knowledgeDescribe: String, fileAddress: String) {
                                    val intent = Intent()
                                    intent.setClass(mContext, WebActivity::class.java)
                                    intent.putExtra("document_url", "http://123.127.109.109:8090/file/temp/a/page1.html")
                                    intent.putExtra("document_title", knowledgeTitle)
                                    intent.putExtra("document_id", knowledgeIds[position])
                                    mContext.startActivity(intent)
                                }
                            })
                        }
                        4 -> {

                        }
                    }


                }

                //配置头部菜单栏
                helper.getView<LinearLayout>(R.id.ll_classify).setOnClickListener {
                    mContext.startActivity(Intent(mContext, ClassifyActivity::class.java))
                }
                helper.getView<LinearLayout>(R.id.ll_video).setOnClickListener {
                    //ChannelActivity.newInstance(mContext, "视频", 1)
                    mContext.startActivity(Intent(mContext, TopicListActivity::class.java))
                }
                helper.getView<LinearLayout>(R.id.ll_document).setOnClickListener {
                    ChannelActivity.newInstance(mContext, "文稿", 3)
                }
                helper.getView<LinearLayout>(R.id.ll_audio).setOnClickListener {
                    ChannelActivity.newInstance(mContext, "音频", 2)
                }
            }
            FIND_TYPE_COMMEND_TOPIC -> {
                val rvCommendTopicList = helper.getView<RecyclerView>(R.id.rv_item_list)
                rvCommendTopicList.layoutManager = LinearLayoutManager(mContext)
                val commendTopicAdapter = CommendTopicAdapter(item!!.data[helper.adapterPosition - 4].commendTopicData, R.layout.child_item_find_commend_topic_layout)
                rvCommendTopicList.adapter = commendTopicAdapter
                rvCommendTopicList.addItemDecoration(DividerItemNoEndDecoration(mContext, LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(mContext, R.color.common_transparent20)))
                helper.getView<TextView>(R.id.tv_all).setOnClickListener {
                    mContext.startActivity(Intent(mContext, TopicListActivity::class.java))
                }
                commendTopicAdapter.setOnItemClickListener { _, _, position ->
                    val topicIntent = Intent(mContext, TopicActivity::class.java)
                    topicIntent.putExtra("TOPIC_ID", item.data[helper.adapterPosition - 4].commendTopicData[position].topicId)
                    topicIntent.putExtra("TOPIC_NAME", item.data[helper.adapterPosition - 4].commendTopicData[position].topicTitle)
                    topicIntent.putExtra("TOPIC_IMAGE", item.data[helper.adapterPosition - 4].commendTopicData[position].imageUrl)
                    topicIntent.putExtra("TOPIC_DESCRIBE", item.data[helper.adapterPosition - 4].commendTopicData[position].topicDescription)
                    topicIntent.putExtra("TOPIC_PRAISE_NUM", item.data[helper.adapterPosition - 4].commendTopicData[position].topicPraiseNum)
                    topicIntent.putExtra("TOPIC_IS_PRAISE", item.data[helper.adapterPosition - 4].commendTopicData[position].topicIsPraise)

                    mContext.startActivity(topicIntent)
                }
                // helper.addOnClickListener(R.id.rl_change)
            }

            FIND_TYPE_COMMEND_VIDEO -> {
                val rvCommendVideoList = helper.getView<RecyclerView>(R.id.rv_item_list)

                rvCommendVideoList.layoutManager = LinearLayoutManager(mContext)

                val commendVideoAdapter = CommendVideoAdapter(item!!.data[helper.adapterPosition - 3].commendVideoData, R.layout.child_item_find_commend_video_layout)

                rvCommendVideoList.adapter = commendVideoAdapter

                //rvCommendVideoList.addItemDecoration(DividerItemNoEndDecoration(mContext, LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(mContext, R.color.common_transparent20)))

                commendVideoAdapter.setOnItemClickListener { adapter, view, position ->
                    //ToastUtils.showShort(item.data[helper.adapterPosition - 2].commendVideoData[position].videoUrl)
                    val intent = Intent()
                    intent.setClass(mContext, VideoActivity::class.java)

                    intent.putExtra("video_url", REQUEST_SOURCE + item.data[helper.adapterPosition - 3].commendVideoData[position].videoUrl)
                    intent.putExtra("video_id", item.data[helper.adapterPosition - 3].commendVideoData[position].id)
                    intent.putExtra("video_author", item.data[helper.adapterPosition - 3].commendVideoData[position].author)
                    mContext.startActivity(intent)
                }

                helper.getView<TextView>(R.id.tv_all).setOnClickListener {
                    ChannelActivity.newInstance(mContext, "全部视频", 1)
                }
                helper.addOnClickListener(R.id.rl_change)
            }

            FIND_TYPE_COMMEND_AUDIO -> {
                var rvCommendAudioList = helper.getView<RecyclerView>(R.id.rv_item_list)

                rvCommendAudioList.layoutManager = LinearLayoutManager(mContext)

                val commendAudioAdapter = CommendAudioAdapter(item!!.data[helper.adapterPosition - 1].commendAudioData, R.layout.child_item_find_commend_audio_layout)

                rvCommendAudioList.adapter = commendAudioAdapter

                //rvCommendAudioList.addItemDecoration(DividerItemNoEndDecoration(mContext, LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(mContext, R.color.common_transparent20)))

                commendAudioAdapter.setOnItemClickListener { adapter, view, position ->
                    val audioEntity = AudioEntity(item.data[helper.adapterPosition - 1].commendAudioData[position].musicId, REQUEST_SOURCE + item.data[helper.adapterPosition - 1].commendAudioData[position].audioUrl, item.data[helper.adapterPosition - 1].commendAudioData[position].title, NetConstant.REQUEST_SOURCE + item.data[helper.adapterPosition - 1].commendAudioData[position].imageUrl)
                    mItemChildClickListener.setOnAudioItemChildClickListener(audioEntity)

//                    if (ServiceUtils.isServiceRunning(OldMusicService::class.java)) {
//                        ServiceUtils.stopService(OldMusicService::class.java)
//                    }
//
//                    val intent = Intent()
//                    intent.setClass(mContext, OldMusicService::class.java)
//                    intent.putExtra("MUSIC_ID", item.data[helper.adapterPosition - 1].commendAudioData[position].musicId)
//                    intent.putExtra("MUSIC_PATH", item.data[helper.adapterPosition - 1].commendAudioData[position].audioUrl)
//                    intent.putExtra("MUSIC_NAME", item.data[helper.adapterPosition - 1].commendAudioData[position].title)
//                    intent.putExtra("MUSIC_AVATAR", NetConstant.REQUEST_SOURCE + item.data[helper.adapterPosition - 1].commendAudioData[position].imageUrl)
//                    intent.putExtra("music_state", true)
//                    mContext.startService(intent)
//                    RxBus.getDefault().post(AudioLayoutVisibleStateEvent(Constant.AUDIO_LAYOUT_VISIBLE_FLAG, true))
//                    RxBus.getDefault().post(MusicPlayNameEvent(0xff21, item.data[helper.adapterPosition - 1].commendAudioData[position].title, NetConstant.REQUEST_SOURCE + item.data[helper.adapterPosition - 1].commendAudioData[position].imageUrl))
//                    RxBus.getDefault().post(MusicPlayStateEvent(0x9898, false))
                }

                helper.getView<TextView>(R.id.tv_all).setOnClickListener {
                    ChannelActivity.newInstance(mContext, "全部音频", 2)
                }

                helper.addOnClickListener(R.id.rl_change)
            }

            FIND_TYPE_COMMEND_DOCUMENT -> {
                var rvCommendDocumentList = helper.getView<RecyclerView>(R.id.rv_item_list)

                rvCommendDocumentList.layoutManager = LinearLayoutManager(mContext)

                val commendDocumentAdapter = CommendDocumentAdapter(item!!.data[helper.adapterPosition - 2].commendDocumentData, R.layout.child_item_find_commend_document_layout)

                rvCommendDocumentList.adapter = commendDocumentAdapter

                //rvCommendDocumentList.addItemDecoration(DividerItemNoEndDecoration(mContext, LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(mContext, R.color.common_transparent20)))

                commendDocumentAdapter.setOnItemClickListener { adapter, view, position ->
                    val intent = Intent()
                    intent.setClass(mContext, WebActivity::class.java)
                    intent.putExtra("document_url", REQUEST_SOURCE + item.data[helper.adapterPosition - 2].commendDocumentData[position].documentUrl)
                    intent.putExtra("document_title", item.data[helper.adapterPosition - 2].commendDocumentData[position].title)
                    intent.putExtra("document_id", item.data[helper.adapterPosition - 2].commendDocumentData[position].id)
                    mContext.startActivity(intent)
                }

                helper.getView<TextView>(R.id.tv_all).setOnClickListener {
                    ChannelActivity.newInstance(mContext, "全部文稿", 3)
                }
                helper.addOnClickListener(R.id.rl_change)
            }
        }
    }


    class CommendTopicAdapter(data: ArrayList<FindCommendTopicEntity>, resLayout: Int) : BaseQuickAdapter<FindCommendTopicEntity, BaseViewHolder>(resLayout, data) {
        override fun convert(helper: BaseViewHolder?, item: FindCommendTopicEntity?) {
            GlideUtils.showImageFade(mContext, REQUEST_SOURCE + item!!.imageUrl, helper!!.getView(R.id.riv_topic_img))
            helper.setText(R.id.tv_topic_name, item.topicTitle)
            helper.setText(R.id.tv_topic_description, item.topicDescription)
            //helper.setText(R.id.tv_topic_newest, item.newestTitle)
            helper.setText(R.id.tv_update_date, "${TimeUtils.millis2String(item.updateDate)}更新")
        }

    }

    inner class CommendVideoAdapter(data: ArrayList<FindCommendVideoEntity>, resLayout: Int) : BaseQuickAdapter<FindCommendVideoEntity, BaseViewHolder>(resLayout, data) {
        override fun convert(helper: BaseViewHolder?, item: FindCommendVideoEntity?) {
            GlideUtils.showImageFade(mContext, REQUEST_SOURCE + item!!.imageUrl, helper!!.getView(R.id.riv_video_img))
            helper.setText(R.id.tv_topic_name, item.title)
            helper.setText(R.id.tv_topic_description, item.author + "|" + TimeUtils.millis2String(item.createDate, SimpleDateFormat("MM-dd")))
        }

    }

    inner class CommendAudioAdapter(data: ArrayList<FindCommendAudioEntity>, resLayout: Int) : BaseQuickAdapter<FindCommendAudioEntity, BaseViewHolder>(resLayout, data) {
        override fun convert(helper: BaseViewHolder?, item: FindCommendAudioEntity?) {
            GlideUtils.showImage(mContext, REQUEST_SOURCE + item!!.imageUrl, helper!!.getView(R.id.civ_audio))
            helper.setText(R.id.tv_topic_name, item.title)
            helper.setText(R.id.tv_topic_description, item.authorName + "|" + TimeUtils.millis2String(item.createDate, SimpleDateFormat("MM-dd")))
        }

    }


    inner class CommendDocumentAdapter(data: ArrayList<FindCommendDocumentEntity>, resLayout: Int) : BaseQuickAdapter<FindCommendDocumentEntity, BaseViewHolder>(resLayout, data) {
        @SuppressLint("SimpleDateFormat")
        override fun convert(helper: BaseViewHolder?, item: FindCommendDocumentEntity?) {
            helper!!.setText(R.id.tv_title, item!!.title)
            helper.setText(R.id.tv_content, item.knowledgeDescribe)
            helper.setText(R.id.tv_writer, item.writer)
            helper.setText(R.id.tv_date, TimeUtils.millis2String(item.date, SimpleDateFormat("MM-dd")))
        }

    }


    interface ItemChildClickListener {
        fun setOnAudioItemChildClickListener(audioEntity: AudioEntity)
    }
}